using Microsoft.AspNetCore.Mvc;
using Lzfy_His_Service.Services;

namespace Lzfy_His_Service.Controllers
{
    [ApiController]
    [Route("api/test/[controller]")]
    public class TestCaptchaController : ControllerBase
    {
        private readonly ICaptchaService _captchaService;

        public TestCaptchaController(ICaptchaService captchaService)
        {
            _captchaService = captchaService;
        }

        [HttpGet("generate")]
        public async Task<IActionResult> GenerateCaptcha()
        {
            var sessionId = Guid.NewGuid().ToString();
            var captchaImage = await _captchaService.GenerateCaptchaAsync(sessionId);
            
            return Ok(new 
            { 
                SessionId = sessionId, 
                CaptchaImage = captchaImage,
                Message = "LazyCaptcha generated successfully"
            });
        }

        [HttpGet("generate-bytes")]
        public async Task<IActionResult> GenerateCaptchaBytes()
        {
            var sessionId = Guid.NewGuid().ToString();
            var captchaBytes = await _captchaService.GenerateCaptchaImageBytesAsync(sessionId);
            
            Response.Headers.Append("X-Session-Id", sessionId);
            return File(captchaBytes, "image/png");
        }

        [HttpGet("generate-with-session")]
        public async Task<IActionResult> GenerateCaptchaWithSession()
        {
            var sessionId = Guid.NewGuid().ToString();
            var captchaBytes = await _captchaService.GenerateCaptchaImageBytesAsync(sessionId);
            
            return Ok(new 
            { 
                SessionId = sessionId,
                CaptchaImage = "data:image/png;base64," + Convert.ToBase64String(captchaBytes),
                Message = "LazyCaptcha with session generated successfully"
            });
        }

        [HttpGet("generate-swagger")]
        public async Task<IActionResult> GenerateCaptchaForSwagger()
        {
            var sessionId = Guid.NewGuid().ToString();
            var captchaBytes = await _captchaService.GenerateCaptchaImageBytesAsync(sessionId);
            
            return Ok(new 
            { 
                SessionId = sessionId,
                CaptchaImage = "data:image/png;base64," + Convert.ToBase64String(captchaBytes),
                Message = "LazyCaptcha for Swagger generated successfully"
            });
        }

        [HttpPost("validate")]
        public async Task<IActionResult> ValidateCaptcha([FromBody] ValidateRequest request)
        {
            var isValid = await _captchaService.ValidateCaptchaAsync(request.SessionId, request.Captcha);
            
            return Ok(new 
            { 
                IsValid = isValid,
                Message = isValid ? "验证码验证成功" : "验证码验证失败"
            });
        }

        public class ValidateRequest
        {
            public string SessionId { get; set; } = string.Empty;
            public string Captcha { get; set; } = string.Empty;
        }
    }
}